//
//  TKIMMenuView.m
//  TKIM
//
//  Created by liupm on 15/11/20.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMMenuBar.h"

@interface TKIMMenuBar()
@property(nonatomic,strong) UIButton *switchBar;
/**当前弹出的子菜单视图*/
@property (nonatomic,strong)TKIMPopView * popView;
/**当前选择的主菜单按钮*/
@property (nonatomic,strong)UIButton * selectedBtn;
@end

@implementation TKIMMenuBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _itemList = [NSMutableArray array];
    }
    return self;
}

-(void)setItemList:(NSArray *)itemList
{
    _itemList = itemList;
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect
{
    [self addSubview:self.switchBar];
    CGFloat scale = [[UIScreen mainScreen] scale];
    CGFloat lineWidth = scale > 0.0 ? 1.0 / scale : 1.0;
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(self.switchBar.right, 0, lineWidth, self.height)];
    line.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:line];
    
    CGFloat width = self.width - self.switchBar.width-_itemList.count*lineWidth;
    CGFloat x = line.right;
    for(TKIMMenuBarItem *item in _itemList)
    {
        NSInteger index = [_itemList indexOfObject:item];
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, width/_itemList.count, self.height)];
        [button setTitle:item.menu_name forState:UIControlStateNormal];
        [button setTitleColor:item.textColor forState:UIControlStateNormal];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        if ([item.type isEqualToString:@"click"]&& item.subMenus.count > 0)
        {
            [button setImage:TKIMGetImage(@"im_Mode_textmenuicon") forState:UIControlStateNormal];
        }
//        [button setImage:TKIMGetImage(@"Menu_icon_list") forState:UIControlStateNormal];
//        [button setImage:TKIMGetImage(@"Menu_icon_list_pressed") forState:UIControlStateHighlighted];
        
        button.titleLabel.font = item.font;
        button.titleLabel.adjustsFontSizeToFitWidth = YES;
        button.tag =  index;
        [button addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        
        if (index != _itemList.count-1)
        {
            UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(button.right, 0, lineWidth, self.height)];
            line.backgroundColor = [UIColor lightGrayColor];
            [self addSubview:line];
        }
        x = button.right+lineWidth;
    }
}

-(UIButton *)switchBar
{
    if (!_switchBar)
    {
        _switchBar = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, self.height)];
        [_switchBar setImage:TKIMGetImage(@"im_but_keyboardUp") forState:UIControlStateNormal];
        [_switchBar addTarget:self action:@selector(switchToolBar) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchBar;
}


-(void)switchToolBar
{
    [self routerEventWithName:TKIMSwitchTooBarEvent userInfo:nil];
}


/**
 *  @Author lpm, 15-11-20 13:11:06
 *
 *  @brief  按钮点击事件
 *
 *  @param button
 */
-(void)doAction:(UIButton *)button
{
    TKIMMenuBarItem * oldItem = self.itemList[self.selectedBtn.tag];
    CGFloat moveHeight =  oldItem.subMenus.count * kSubButtonHeight;
    [self.popView hideSubMenuWithAnimationHeight:moveHeight];
    
    if (button==self.selectedBtn)
    {
        button.selected = !button.selected;
    }
    else
    {
        self.selectedBtn.selected = NO;
        button.selected = YES;
    }
    
    self.selectedBtn = button;
    NSInteger index = button.tag;
    TKIMMenuBarItem *item = self.itemList[index];
    //先将原先的子菜单隐藏
    if ([item.type isEqualToString:@"click"]&& item.subMenus.count > 0)
    {
        if (button.selected)
        {
            self.popView  = [[TKIMPopView alloc] init];
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidePopView)];
            [self.popView addGestureRecognizer:tap];
            
            //TKIMChatView视图
            UIView * chatView = self.superview.superview;
            self.popView.frame = chatView.bounds;
            [chatView insertSubview:self.popView belowSubview:self.superview];
            
            //转换到TKIMChatView的坐标系中
            CGRect btnFrame = [button convertRect:button.bounds toView:self.superview.superview];
            [self.popView showSubMenuViewWithSelectedButtonFrame:btnFrame andSubMenus:item.subMenus];
            self.popView.menuButton = self.selectedBtn;
        }
    }
    else
    {
        if ([item.type isEqualToString:@"link"])
        {
            [self routerEventWithName:TKIMSubMenuClickEvent userInfo:item];
        }
        else if([item.type isEqualToString:@"click"])
        {
            [self routerEventWithName:TKIMMenuBarOnclickEvent userInfo:item.key];
        }
    }
}


/**
 *  隐藏弹出的子菜单
 */
-(void)hidePopView
{
    self.selectedBtn.selected = NO;
    if (self.selectedBtn&&self.itemList.count>0)
    {
        TKIMMenuBarItem * oldItem = self.itemList[self.selectedBtn.tag];
        CGFloat moveHeight =  oldItem.subMenus.count * kSubButtonHeight;
        [self.popView hideSubMenuWithAnimationHeight:moveHeight];
    }
}

@end

@implementation TKIMMenuBarItem
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        _textColor = [UIColor grayColor];
        _font = [UIFont systemFontOfSize:15.0f];
    }
    return self;
}


-(NSMutableArray *)subMenus
{
    if (!_subMenus)
    {
        _subMenus = [NSMutableArray array];
    }
    return _subMenus;
}

-(void)setValue:(id)value forKey:(NSString *)key
{
    if ([key isEqualToString:@"sub_menus"])
    {
        for (NSDictionary * subDic in value)
        {
            TKIMMenuBarItem * subItem = [[TKIMMenuBarItem alloc] init];
            [subItem setValuesForKeysWithDictionary:subDic];
            [self.subMenus addObject:subItem];
        }
    }
    else
    {
        [super setValue:value forKey:key];
    }
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}

-(id)valueForKey:(NSString *)key
{
    return nil;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@", _menu_name];
}


@end
